﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 资源管理，加载资源
/// </summary>
public class ResourcesManager : MonoSingleton<ResourcesManager> {
    /// <summary>
    /// 存放加载资源的字典
    /// </summary>
    private Dictionary<string, Object> objectDictionary = new Dictionary<string, Object>();
    /// <summary>
    /// 存放一个目录下的资源
    /// </summary>
    private Dictionary<string, Dictionary<string, Object>> objectArrayDictionary = new Dictionary<string, Dictionary<string, Object>>();
    /// <summary>
    /// 加载(单个)资源，如果资源已经存在则返回已有的
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"></param>
    /// <returns></returns>
    public T Load<T>(string path) where T : Object
    {
        string key = path;
        if (objectDictionary.ContainsKey(key))
        {
            return objectDictionary[key] as T;
        }
        Object res = Resources.Load<T>(path) as T;
        objectDictionary.Add(key, res);
        if (res == null)
        {
            Debug.Log("没有该路径对应的物体");
        }
        return res as T;
    }
    public Dictionary<string,T> LoadAll<T>(string path) where T : Object
    {
        string key1 = path;
        if (objectArrayDictionary.ContainsKey(key1))
        {
            return objectArrayDictionary[key1]as Dictionary<string,T>;
        }
        Object[] res = Resources.LoadAll<T>(path);
        Dictionary<string, T> dic = new Dictionary<string, T>();
        foreach(Object o in res)
        {
            dic.Add(o.name, o as T);
        }
        objectArrayDictionary.Add(key1, dic as Dictionary<string,Object>);
        return dic;
    }

}
